package com.cskaoyan.service.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.bo.WXGoodListBO;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.WXGoodsService;
import com.cskaoyan.utils.QueryUserIdByUserNameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WXGoodsServiceImpl implements WXGoodsService {
    @Autowired
    CskaoyanmallGoodsMapper goodsMapper;
    @Autowired
    CskaoyanmallCategoryMapper categoryMapper;
    @Autowired
    CskaoyanmallCommentMapper commentMapper;
    @Autowired
    CskaoyanmallGoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    CskaoyanmallGoodsProductMapper goodsProductMapper;
    @Autowired
    CskaoyanmallGoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    CskaoyanmallIssueMapper issueMapper;
    @Autowired
    CskaoyanmallGrouponMapper grouponMapper;
    @Autowired
    CskaoyanmallBrandMapper brandMapper;
    @Autowired
    CskaoyanmallCollectMapper collectMapper;
    @Autowired
    CskaoyanmallSearchHistoryMapper searchHistoryMapper;
    @Autowired
    CskaoyanmallUserMapper userMapper;

    @Override
    public Map<String, Long> count() {
        HashMap<String, Long> map = new HashMap<>( );

        CskaoyanmallGoodsExample goodsExample = new CskaoyanmallGoodsExample( );
        long l = goodsMapper.countByExample(goodsExample);
        map.put("goodsCount", l);
        return map;
    }

    @Override
    public WXGoodsListVO list(WXGoodListBO wxGoodListBO) {

        String isHot = wxGoodListBO.getIsHot( );
        String isNew = wxGoodListBO.getIsNew( );
        Integer brandId = wxGoodListBO.getBrandId( );
        Integer page = wxGoodListBO.getPage( );
        Integer size = wxGoodListBO.getSize( );
        Integer categoryId = wxGoodListBO.getCategoryId( );
        if (categoryId == null) {
            categoryId = 0;
        }
        String keyword = wxGoodListBO.getKeyword( );
        String sort = wxGoodListBO.getSort( );
        String order = wxGoodListBO.getOrder( );

        WXGoodsListVO listVO = new WXGoodsListVO( );
        CskaoyanmallGoodsExample goodsExample = new CskaoyanmallGoodsExample( );
        CskaoyanmallGoodsExample.Criteria criteria = goodsExample.createCriteria( );

        if (sort != null && order != null) {
            if (("true").equals(isHot)) {
                criteria.andIsHotEqualTo(true);
            } else if (("true").equals(isNew)) {
                criteria.andIsNewEqualTo(true);
            } else if (brandId != null) {
                criteria.andBrandIdEqualTo(brandId);
            } else if (keyword != null) {
                setSearchHistory(keyword);
                criteria.andNameLike("%" + keyword + "%");
            }
            goodsExample.setOrderByClause(sort + " " + order);
        }
        listVO = getListVO(goodsExample);
        if (categoryId != 0) {
            criteria.andCategoryIdEqualTo(categoryId);
            listVO.setGoodsList(getListVO(goodsExample).getGoodsList( ));
            listVO.setCount(getListVO(goodsExample).getCount( ));
        }
        return listVO;
    }

    @Override
    public WXGoodsCategoryVO category(Integer id) {
        WXGoodsCategoryVO categoryVO = new WXGoodsCategoryVO( );

        CskaoyanmallCategoryExample categoryExample = new CskaoyanmallCategoryExample( );
        CskaoyanmallCategoryExample.Criteria criteria = categoryExample.createCriteria( );

        criteria.andIdEqualTo(id);
        List<CskaoyanmallCategory> categories = categoryMapper.selectByExample(categoryExample);
        CskaoyanmallCategory category = categories.get(0);
        if (category.getPid( ) == 0) {
            CskaoyanmallCategoryExample categoryExample1 = new CskaoyanmallCategoryExample( );
            categoryExample1.createCriteria( ).andPidEqualTo(id);
            List<CskaoyanmallCategory> categories1 = categoryMapper.selectByExample(categoryExample1);
            category = categories1.get(0);
        }

        categoryVO.setCurrentCategory(category);

        Integer pid = category.getPid( );

        CskaoyanmallCategoryExample categoryExample1 = new CskaoyanmallCategoryExample( );
        categoryExample1.createCriteria( ).andIdEqualTo(pid);
        List<CskaoyanmallCategory> categories1 = categoryMapper.selectByExample(categoryExample1);
        CskaoyanmallCategory category1 = categories1.get(0);
        categoryVO.setParentCategory(category1);

        CskaoyanmallCategoryExample categoryExample2 = new CskaoyanmallCategoryExample( );
        categoryExample2.createCriteria( ).andPidEqualTo(pid);
        List<CskaoyanmallCategory> categoryList = categoryMapper.selectByExample(categoryExample2);
        categoryVO.setBrotherCategory(categoryList);

        return categoryVO;
    }

    @Override
    public WXGoodsDetailVO detail(Integer id) {
        WXGoodsDetailVO wxGoodsDetailVO = new WXGoodsDetailVO( );

        WXGoodsCommentVO wxGoodsCommentVO = new WXGoodsCommentVO( );

        WXGoodsSpeVO wxgoodsSpeVO = new WXGoodsSpeVO( );

        CskaoyanmallCollectExample collectExample = new CskaoyanmallCollectExample( );
        collectExample.createCriteria( ).andValueIdEqualTo(id);
        long userHasCollect = collectMapper.countByExample(collectExample);
        wxGoodsDetailVO.setUserHasCollect(userHasCollect);

        CskaoyanmallGoodsExample goodsExample = new CskaoyanmallGoodsExample( );
        goodsExample.createCriteria( ).andIdEqualTo(id);
        List<CskaoyanmallGoods> goods = goodsMapper.selectByExample(goodsExample);
        CskaoyanmallGoods good = goods.get(0);
        wxGoodsDetailVO.setInfo(good);

        CskaoyanmallIssueExample issueExample = new CskaoyanmallIssueExample( );
        List<CskaoyanmallIssue> issues = issueMapper.selectByExample(issueExample);
        wxGoodsDetailVO.setIssue(issues);

        Integer brandId = good.getBrandId( );
        CskaoyanmallBrandExample brandExample = new CskaoyanmallBrandExample( );
        brandExample.createCriteria( ).andIdEqualTo(brandId);
        List<CskaoyanmallBrand> brands = null;
        CskaoyanmallBrand brand = null;
        try {
            brands = brandMapper.selectByExample(brandExample);
            brand = brands.get(0);
        } catch (Exception e) {
            brand = new CskaoyanmallBrand( );
        }
        wxGoodsDetailVO.setBrand(brand);

        CskaoyanmallGoodsProductExample productExample = new CskaoyanmallGoodsProductExample( );
        productExample.createCriteria( ).andGoodsIdEqualTo(id);
        List<CskaoyanmallGoodsProduct> productList = goodsProductMapper.selectByExample(productExample);
        wxGoodsDetailVO.setProductList(productList);

        CskaoyanmallGoodsAttributeExample attributeExample = new CskaoyanmallGoodsAttributeExample( );
        attributeExample.createCriteria( ).andGoodsIdEqualTo(id);
        List<CskaoyanmallGoodsAttribute> attributes = goodsAttributeMapper.selectByExample(attributeExample);
        wxGoodsDetailVO.setAttribute(attributes);

        CskaoyanmallCommentExample commentExample = new CskaoyanmallCommentExample( );
        commentExample.createCriteria( ).andValueIdEqualTo(id);
        long commentCount = commentMapper.countByExample(commentExample);
        wxGoodsCommentVO.setCount(commentCount);

        List<WXGoodsCommentDataVO> dataVOS = commentMapper.selectDataByGoods( );
        wxGoodsCommentVO.setData(dataVOS);
        wxGoodsDetailVO.setComment(wxGoodsCommentVO);

        CskaoyanmallGoodsSpecificationExample specificationExample = new CskaoyanmallGoodsSpecificationExample( );
        specificationExample.createCriteria( ).andGoodsIdEqualTo(id);
        List<CskaoyanmallGoodsSpecification> specificationList = goodsSpecificationMapper.selectByExample(specificationExample);

        wxgoodsSpeVO.setValueList(specificationList);
        List<WXGoodsSpeVO> objects = new ArrayList<>( );
        objects.add(wxgoodsSpeVO);
        wxGoodsDetailVO.setSpecificationList(objects);
        return wxGoodsDetailVO;
    }

    @Override
    public WXGoodRelatedListVO related(Integer id) {
        WXGoodRelatedListVO relatedListVO = new WXGoodRelatedListVO( );
        CskaoyanmallGoodsExample goodsExample = new CskaoyanmallGoodsExample( );
        goodsExample.createCriteria( ).andIdEqualTo(id);
        List<CskaoyanmallGoods> goods = goodsMapper.selectByExample(goodsExample);
        CskaoyanmallGoods good = goods.get(0);
        Integer categoryId = good.getCategoryId( );
        List<WXGoodsRelatedVO> wxGoodsRelatedVOS = goodsMapper.selectGoodsRelated(categoryId);
        relatedListVO.setGoodsList(wxGoodsRelatedVOS.subList(0, 4));
        return relatedListVO;
    }

    private WXGoodsListVO getListVO(CskaoyanmallGoodsExample goodsExample) {
        WXGoodsListVO wxGoodsListVO = new WXGoodsListVO( );
        List<CskaoyanmallGoods> goodsList = goodsMapper.selectByExample(goodsExample);

        wxGoodsListVO.setGoodsList(goodsList);

        long l = goodsMapper.countByExample(goodsExample);
        wxGoodsListVO.setCount(l);

        List<CskaoyanmallCategory> categoryList = new ArrayList<>( );
        for (CskaoyanmallGoods good :
                goodsList) {
            Integer goodCategoryId = good.getCategoryId( );
            CskaoyanmallCategoryExample categoryExample = new CskaoyanmallCategoryExample( );
            categoryExample.createCriteria( ).andIdEqualTo(goodCategoryId);
            List<CskaoyanmallCategory> categories = categoryMapper.selectByExample(categoryExample);
            CskaoyanmallCategory category = categories.get(0);
            if (!categoryList.contains(category)) {
                categoryList.add(categories.get(0));
            }
        }
        wxGoodsListVO.setFilterCategoryList(categoryList);

        return wxGoodsListVO;
    }

    private void setSearchHistory(String keyword) {
        Integer userId = QueryUserIdByUserNameUtils.queryName(userMapper);

        String from = "wx";
        CskaoyanmallSearchHistoryExample historyExample = new CskaoyanmallSearchHistoryExample( );
        historyExample.createCriteria( ).andKeywordEqualTo(keyword);
        List<CskaoyanmallSearchHistory> historyList = searchHistoryMapper.selectByExample(historyExample);
        if (historyList == null || historyList.size( ) == 0) {
            CskaoyanmallSearchHistory searchHistory = new CskaoyanmallSearchHistory( );
            searchHistory.setKeyword(keyword);
            searchHistory.setFrom(from);
            searchHistory.setUserId(userId);
            searchHistory.setAddTime(new Date());
            searchHistory.setDeleted(false);
            searchHistoryMapper.insert(searchHistory);
        } else {
            CskaoyanmallSearchHistory history = new CskaoyanmallSearchHistory( );
            history.setDeleted(false);
            searchHistoryMapper.updateByExampleSelective(history, historyExample);
        }
    }
}